package com.eric.log.mapred;

import java.io.IOException;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class LogBasicMapRed {
	public static int fn_k;
	public static int std_fn = 12;
	public static String fs = ",";
	
	private static boolean sessionAsKey = false;
	private static String sessionUV_fs = ".";
	private static int session_fn = 1;
	
	public static class BasicMapper extends Mapper<Object, Text, Text, Text>{
		public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
			if (StringUtils.countMatches(value.toString(), fs) == std_fn) {
				String k = value.toString().split(fs)[fn_k];
		    	String v = value.toString().replaceAll(fs+k, "");
		    	
		    	if (sessionAsKey) k = value.toString().split(fs)[fn_k].split(sessionUV_fs)[session_fn];
		    	
		    	context.write(new Text(k),new Text(v));
			}
		}
	}
	
	public static void sessionAsKey (boolean yesNo) {
		sessionAsKey = yesNo;
	}
	
	public static void setSessionUVFieldSeparator (String separator) {
		sessionUV_fs = separator;
	}
	
	public static void setSessionFiendNum (int sessionFieldNum) {
		session_fn = sessionFieldNum;
	}
	
	public static void setKeyFieldNum (int keyFieldNum) {
		fn_k = keyFieldNum;
	}
	
	public static void setFieldSeparator (String FieldSeparator) {
		fs = FieldSeparator;
	}
	
	public static void setStdFieldNum (int stdFieldNum) {
		std_fn = stdFieldNum;
	}
	
	public static void run () {
		
	}
}
